import request from './axios';

export interface IInventoryDto {
  id?: number;
  productId: string;
  productName?: string; // 前端展示用，后端接口可能不返回此字段
  quantity: number;
  reservedQuantity: number;
  availableQuantity: number;
  safetyStock: number;
  warehouseId: string;
  warehouseName?: string; // 前端展示用，后端接口可能不返回此字段
  location: string;
  lastUpdated?: string;
  batchNumber?: string;
  expiryDate?: string;
}

export interface IInventoryRequest {
  productId: string;
  quantity: number;
  reservedQuantity?: number;
  safetyStock: number;
  warehouseId: string;
  location?: string;
  batchNumber?: string;
  expiryDate?: string;
}

export interface IInventoryQuery {
  page?: number;
  size?: number;
  sort?: string;
  direction?: string;
  productId?: string;
  warehouseId?: string;
  lowStock?: boolean;
}

const inventoryApi = {
  // 获取所有库存
  getAllInventories: () => {
    return request({
      url: '/inventory',
      method: 'get'
    });
  },
  
  // 获取指定产品的库存
  getProductInventory: (productId: string) => {
    return request({
      url: `/inventory/product/${productId}`,
      method: 'get'
    });
  },
  
  // 获取指定仓库的库存
  getWarehouseInventories: (warehouseId: string) => {
    return request({
      url: `/inventory/warehouse/${warehouseId}`,
      method: 'get'
    });
  },
  
  // 获取库存不足的商品
  getLowStockItems: () => {
    return request({
      url: '/inventory/lowstock',
      method: 'get'
    });
  },
  
  // 获取有库存的商品
  getAvailableItems: () => {
    return request({
      url: '/inventory/available',
      method: 'get'
    });
  },
  
  // 创建库存记录
  createInventory: (data: IInventoryRequest) => {
    return request({
      url: '/inventory',
      method: 'post',
      data
    });
  },
  
  // 更新库存记录
  updateInventory: (id: number, data: IInventoryRequest) => {
    return request({
      url: `/inventory/${id}`,
      method: 'put',
      data
    });
  },
  
  // 删除库存记录
  deleteInventory: (id: number) => {
    return request({
      url: `/inventory/${id}`,
      method: 'delete'
    });
  },
  
  // 增加库存
  addStock: (productId: string, quantity: number) => {
    return request({
      url: `/inventory/product/${productId}/add`,
      method: 'put',
      params: { quantity }
    });
  },
  
  // 减少库存
  reduceStock: (productId: string, quantity: number) => {
    return request({
      url: `/inventory/product/${productId}/reduce`,
      method: 'put',
      params: { quantity }
    });
  },
  
  // 预留库存
  reserveStock: (productId: string, quantity: number) => {
    return request({
      url: `/inventory/product/${productId}/reserve`,
      method: 'put',
      params: { quantity }
    });
  },
  
  // 释放预留库存
  releaseReservedStock: (productId: string, quantity: number) => {
    return request({
      url: `/inventory/product/${productId}/release`,
      method: 'put',
      params: { quantity }
    });
  }
};

export default inventoryApi; 